package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MapHprMprRangeTable {
  private static final Log _log = LogFactory.getLog(MapHprMprRangeTable.class);
  
  private static final Map<Integer, MapHprMpr> _maphprmprList = new HashMap<>();
  
  private static MapHprMprRangeTable _instance;
  
  private MapHprMprRangeTable() {
    load();
  }
  
  public static MapHprMprRangeTable get() {
    if (_instance == null)
      _instance = new MapHprMprRangeTable(); 
    return _instance;
  }
  
  public void load() {
    Connection co = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      ps = co.prepareStatement("SELECT * FROM `w_HprMprRangeMap`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int mapid = rs.getInt("mapid");
        int minX = rs.getInt("min_x");
        int maxX = rs.getInt("max_x");
        int minY = rs.getInt("min_y");
        int maxY = rs.getInt("max_y");
        int hpr = rs.getInt("hpr");
        int mpr = rs.getInt("mpr");
        MapHprMpr map = new MapHprMpr();
        map._hpr = hpr;
        map._mpr = mpr;
        map.minX = minX;
        map.maxX = maxX;
        map.minY = minY;
        map.maxY = maxY;
        _maphprmprList.put(new Integer(mapid), map);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(co);
    } 
    _log.info("载入地图范围回血回魔:" + _maphprmprList.size());
  }
  
  public int getMapHpr(L1PcInstance pc, int mapid) {
    if (_maphprmprList.containsKey(Integer.valueOf(mapid))) {
      if (pc.getMap().isUnderwater()) {
        if (pc.getInventory().checkEquipped(20207))
          return 0; 
        if (pc.hasSkillEffect(1003))
          return 0; 
        if (pc.getInventory().checkEquipped(21048) && pc.getInventory().checkEquipped(21049) && 
          pc.getInventory().checkEquipped(21050))
          return 0; 
      } 
      if (!checkLoc(pc, mapid).booleanValue())
        return 0; 
      return (_maphprmprList.get(Integer.valueOf(mapid)))._hpr;
    } 
    return 0;
  }
  
  private Boolean checkLoc(L1PcInstance pc, int mapid) {
    MapHprMpr mapHprMpr = _maphprmprList.get(Integer.valueOf(mapid));
    if (pc.getX() < mapHprMpr.minX)
      return Boolean.FALSE; 
    if (pc.getX() > mapHprMpr.maxX)
      return Boolean.FALSE; 
    if (pc.getY() < mapHprMpr.minY)
      return Boolean.FALSE; 
    if (pc.getY() > mapHprMpr.maxY)
      return Boolean.FALSE; 
    return Boolean.TRUE;
  }
  
  public int getMapMpr(L1PcInstance pc, int mapid) {
    if (_maphprmprList.containsKey(Integer.valueOf(mapid)) && checkLoc(pc, mapid).booleanValue())
      return (_maphprmprList.get(Integer.valueOf(mapid)))._mpr; 
    return 0;
  }
  
  private class MapHprMpr {
    private int _hpr;
    
    private int _mpr;
    
    private int minX;
    
    private int maxX;
    
    private int minY;
    
    private int maxY;
    
    private MapHprMpr() {}
  }
}
